﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

/*
 * Image for Easy mode found:
 * Child's Play. Digital image. N.p., n.d. Web. 29 Sept. 2014.
 * <http://www.vtvcab.vn/content/upload/1/home/partnercarousel/thu-n/mabupbe.jpg>.
 * 
 * Image for Medium mode found:
 * Antipova, Lara. Exorcist Regan. Digital image. Img Fave. 
 * Img FAve, 2012. Web. 14 Nov. 2014. <http://imgfave.com/view/3081764>.
 * 
 * Image for Hard mode found:
 * Annabelle. Digital image. Screen Rant. Screen Rant, 9 July 2014. 
 * Web. 14 Nov. 2014. <http://screenrant.com/conjuring-spinoff-annabelle-october-2014/>.
 * */
namespace CSE360_SudokuProject
{
    public partial class MainGameScreen9x9 : Form
    {
        //variables
        LogInMenu loginParent;
        PauseMenu thePause;
        public int currentSelectedNumber;
        Button highlightedInput;
        Button[,] cells = new Button[9,9];
        Random helpButtonRandom = new Random();

        public MainGameScreen9x9(LogInMenu loginparent)
        {
            //intialize the form
            InitializeComponent();
            loginParent = loginparent;
            //sets inputs to nothing
            currentSelectedNumber = 0;
            highlightedInput = Button1;
            //sets necessary data for 16x16 in MainGameInputClass
            MainGameInputClass.the9x9Board = this;
            MainGameInputClass.wrongAnswerCount = 0;
            MainGameInputClass.undoInfoArray[0].undoButton = null;
            MainGameInputClass.undoInfoArray[1].undoButton = null;
            MainGameInputClass.undoInfoArray[2].undoButton = null;

            //selects which background image to use
            if (FileManager.difficulty == 1)
            {
                pictureBox1.Visible = true;
                pictureBox2.Visible = false;
                pictureBox3.Visible = false;
            }
            else if (FileManager.difficulty == 2)
            {
                pictureBox1.Visible = false;
                pictureBox2.Visible = true;
                pictureBox3.Visible = false;
            }
            else if (FileManager.difficulty == 3)
            {
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
                pictureBox3.Visible = true;
            }
            
            
            Time.Text = string.Format("{0:00}:{1:00}", FileManager.currentTimeMinute, FileManager.currentTimeSecond);

            //method calls to load cells into array and data to board
            LoadCells();
            LoadData();
        }       

        #region auxiliary button
        //
        // Pause Button
        //
        private void PauseButton_Click(object sender, EventArgs e)
        {
            //disables this form and hides it
            this.Enabled = false;
            this.Visible = false;
            //creates a pause form and shows it
            thePause = new PauseMenu(loginParent, this, null);
            thePause.Show();
            Timer.Enabled = false;
        }

        //
        // Help Button
        //
        private void HelpButton_Click(object sender, EventArgs e)
        {
            //checks that there is a wrong spot left in the puzzle
            //bit redundent but good safty mesure
            if (MainGameInputClass.wrongAnswerCount > 0)
            {
                //initalize i and j indexes
                int i = helpButtonRandom.Next(9);
                int j = helpButtonRandom.Next(9);

                //finds a cell that is wrong (non null button in wrong cell list)
                while (MainGameInputClass.wrongCellsList[i, j] == null)
                {
                    i = helpButtonRandom.Next(9);
                    j = helpButtonRandom.Next(9);
                }

                //removes the button from wrong list and count
                MainGameInputClass.wrongCellsList[i, j] = null;
                MainGameInputClass.wrongAnswerCount--;
                //updates for the undo
                MainGameInputClass.UpdateUndo(cells[i, j], i, j);
                //updates the board and current board data
                cells[i, j].Text = ThePuzzleGenerator.BoardAnswer9x9[i, j].ToString();
                ThePuzzleGenerator.BoardCurrent9x9[i, j] = ThePuzzleGenerator.BoardAnswer9x9[i, j];
                //decrements the players score
                FileManager.currentScore -= 50;
                //checks if solution is reached      
            }
            //forces board to redraw board
            this.Refresh();

            //checks if the help fixed the last wrong cell
            if (MainGameInputClass.wrongAnswerCount <= 0)
            {               
                System.Threading.Thread.Sleep(1000);
                this.Close();
            }
        }

        //
        // Undo Button
        //
        private void UndoButton_Click(object sender, EventArgs e)
        {
            //calls undo button method in input class
            MainGameInputClass.UndoButton_Clicked();
        }
        #endregion

        #region InputButtons
        //list of method calls to the input class to 
            //update the selected input button
        private void Button1_Click(object sender, EventArgs e)
        {                
            MainGameInputClass.SelectButton(Button1, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button2, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button3, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button4, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button5, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button6, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button7, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button8, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void Button9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(Button9, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }

        private void DeleteButton_Click(object sender, EventArgs e)
        {
            MainGameInputClass.SelectButton(DeleteButton, ref highlightedInput, ref currentSelectedNumber, DefaultBackColor);
        }     
        #endregion

        #region Cells
        //list of methods that calls to the imput class when a cell is clicked
        //to input the selected number

        private void Cell1_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_1, currentSelectedNumber, 0, 0);
        }
        
        private void Cell1_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_2, currentSelectedNumber, 0, 1);
        }

        private void Cell1_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_3, currentSelectedNumber, 0, 2);
        }

        private void Cell1_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_4, currentSelectedNumber, 1, 0);
        }

        private void Cell1_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_5, currentSelectedNumber, 1, 1);
        }

        private void Cell1_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_6, currentSelectedNumber, 1, 2);
        }

        private void Cell1_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_7, currentSelectedNumber, 2, 0);
        }

        private void Cell1_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_8, currentSelectedNumber, 2, 1);
        }

        private void Cell1_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell1_9, currentSelectedNumber, 2, 2);
        }

        private void Cell2_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_1, currentSelectedNumber, 0, 3);
        }

        private void Cell2_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_2, currentSelectedNumber, 0, 4);
        }

        private void Cell2_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_3, currentSelectedNumber, 0, 5);
        }

        private void Cell2_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_4, currentSelectedNumber, 1, 3);
        }

        private void Cell2_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_5, currentSelectedNumber, 1, 4);
        }

        private void Cell2_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_6, currentSelectedNumber, 1, 5);
        }

        private void Cell2_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_7, currentSelectedNumber, 2, 3);
        }

        private void Cell2_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_8, currentSelectedNumber, 2, 4);
        }

        private void Cell2_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell2_9, currentSelectedNumber, 2, 5);
        }

        private void Cell3_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_1, currentSelectedNumber, 0, 6);
        }

        private void Cell3_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_2, currentSelectedNumber, 0, 7);
        }

        private void Cell3_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_3, currentSelectedNumber, 0, 8);
        }

        private void Cell3_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_4, currentSelectedNumber, 1, 6);
        }

        private void Cell3_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_5, currentSelectedNumber, 1, 7);
        }

        private void Cell3_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_6, currentSelectedNumber, 1, 8);
        }

        private void Cell3_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_7, currentSelectedNumber, 2, 6);
        }

        private void Cell3_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_8, currentSelectedNumber, 2, 7);
        }

        private void Cell3_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell3_9, currentSelectedNumber, 2, 8);
        }

        private void Cell4_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_1, currentSelectedNumber, 3, 0);
        }

        private void Cell4_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_2, currentSelectedNumber, 3, 1);
        }

        private void Cell4_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_3, currentSelectedNumber, 3, 2);
        }

        private void Cell4_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_4, currentSelectedNumber, 4, 0);
        }

        private void Cell4_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_5, currentSelectedNumber, 4, 1);
        }

        private void Cell4_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_6, currentSelectedNumber, 4, 2);
        }

        private void Cell4_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_7, currentSelectedNumber, 5, 0);
        }

        private void Cell4_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_8, currentSelectedNumber, 5, 1);
        }

        private void Cell4_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell4_9, currentSelectedNumber, 5, 2);
        }

        private void Cell5_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_1, currentSelectedNumber, 3, 3);
        }

        private void Cell5_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_2, currentSelectedNumber, 3, 4);
        }

        private void Cell5_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_3, currentSelectedNumber, 3, 5);
        }

        private void Cell5_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_4, currentSelectedNumber, 4, 3);
        }

        private void Cell5_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_5, currentSelectedNumber, 4, 4);
        }

        private void Cell5_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_6, currentSelectedNumber, 4, 5);
        }

        private void Cell5_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_7, currentSelectedNumber, 5, 3);
        }

        private void Cell5_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_8, currentSelectedNumber, 5, 4);
        }

        private void Cell5_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell5_9, currentSelectedNumber, 5, 5);
        }

        private void Cell6_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_1, currentSelectedNumber, 3, 6);
        }

        private void Cell6_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_2, currentSelectedNumber, 3, 7);
        }

        private void Cell6_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_3, currentSelectedNumber, 3, 8);
        }

        private void Cell6_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_4, currentSelectedNumber, 4, 6);
        }

        private void Cell6_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_5, currentSelectedNumber, 4, 7);
        }

        private void Cell6_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_6, currentSelectedNumber, 4, 8);
        }

        private void Cell6_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_7, currentSelectedNumber, 5, 6);
        }

        private void Cell6_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_8, currentSelectedNumber, 5, 7);
        }

        private void Cell6_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell6_9, currentSelectedNumber, 5, 8);
        }

        private void Cell7_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_1, currentSelectedNumber, 6, 0);
        }

        private void Cell7_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_2, currentSelectedNumber, 6, 1);
        }

        private void Cell7_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_3, currentSelectedNumber, 6, 2);
        }

        private void Cell7_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_4, currentSelectedNumber, 7, 0);
        }

        private void Cell7_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_5, currentSelectedNumber, 7, 1);
        }

        private void Cell7_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_6, currentSelectedNumber, 7, 2);
        }

        private void Cell7_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_7, currentSelectedNumber, 8, 0);
        }

        private void Cell7_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_8, currentSelectedNumber, 8, 1);
        }

        private void Cell7_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell7_9, currentSelectedNumber, 8, 2);
        }

        private void Cell8_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_1, currentSelectedNumber, 6, 3);
        }

        private void Cell8_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_2, currentSelectedNumber, 6, 4);
        }

        private void Cell8_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_3, currentSelectedNumber, 6, 5);
        }

        private void Cell8_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_4, currentSelectedNumber, 7, 3);
        }

        private void Cell8_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_5, currentSelectedNumber, 7, 4);
        }

        private void Cell8_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_6, currentSelectedNumber, 7, 5);
        }

        private void Cell8_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_7, currentSelectedNumber, 8, 3);
        }

        private void Cell8_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_8, currentSelectedNumber, 8, 4);
        }

        private void Cell8_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell8_9, currentSelectedNumber, 8, 5);
        }

        private void Cell9_1_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_1, currentSelectedNumber, 6, 6);
        }

        private void Cell9_2_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_2, currentSelectedNumber, 6, 7);
        }

        private void Cell9_3_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_3, currentSelectedNumber, 6, 8);
        }

        private void Cell9_4_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_4, currentSelectedNumber, 7, 6);
        }

        private void Cell9_5_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_5, currentSelectedNumber, 7, 7);
        }

        private void Cell9_6_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_6, currentSelectedNumber, 7, 8);
        }

        private void Cell9_7_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_7, currentSelectedNumber, 8, 6);
        }

        private void Cell9_8_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_8, currentSelectedNumber, 8, 7);
        }

        private void Cell9_9_Click(object sender, EventArgs e)
        {
            MainGameInputClass.InputAnswer(Cell9_9, currentSelectedNumber, 8, 8);
        }
        #endregion

        #region load cells (buttons) into a array and then populates the board

        //loads cells into array
        private void LoadCells()
        {
            cells[0,0] = Cell1_1;
            cells[0,1] = Cell1_2;
            cells[0,2] = Cell1_3;
            cells[1,0] = Cell1_4;
            cells[1,1] = Cell1_5;
            cells[1,2] = Cell1_6;
            cells[2,0] = Cell1_7;
            cells[2,1] = Cell1_8;
            cells[2,2] = Cell1_9;

            cells[0, 3] = Cell2_1;
            cells[0, 4] = Cell2_2;
            cells[0, 5] = Cell2_3;
            cells[1, 3] = Cell2_4;
            cells[1, 4] = Cell2_5;
            cells[1, 5] = Cell2_6;
            cells[2, 3] = Cell2_7;
            cells[2, 4] = Cell2_8;
            cells[2, 5] = Cell2_9;

            cells[0, 6] = Cell3_1;
            cells[0, 7] = Cell3_2;
            cells[0, 8] = Cell3_3;
            cells[1, 6] = Cell3_4;
            cells[1, 7] = Cell3_5;
            cells[1, 8] = Cell3_6;
            cells[2, 6] = Cell3_7;
            cells[2, 7] = Cell3_8;
            cells[2, 8] = Cell3_9;

            cells[3, 0] = Cell4_1;
            cells[3, 1] = Cell4_2;
            cells[3, 2] = Cell4_3;
            cells[4, 0] = Cell4_4;
            cells[4, 1] = Cell4_5;
            cells[4, 2] = Cell4_6;
            cells[5, 0] = Cell4_7;
            cells[5, 1] = Cell4_8;
            cells[5, 2] = Cell4_9;

            cells[3, 3] = Cell5_1;
            cells[3, 4] = Cell5_2;
            cells[3, 5] = Cell5_3;
            cells[4, 3] = Cell5_4;
            cells[4, 4] = Cell5_5;
            cells[4, 5] = Cell5_6;
            cells[5, 3] = Cell5_7;
            cells[5, 4] = Cell5_8;
            cells[5, 5] = Cell5_9;

            cells[3, 6] = Cell6_1;
            cells[3, 7] = Cell6_2;
            cells[3, 8] = Cell6_3;
            cells[4, 6] = Cell6_4;
            cells[4, 7] = Cell6_5;
            cells[4, 8] = Cell6_6;
            cells[5, 6] = Cell6_7;
            cells[5, 7] = Cell6_8;
            cells[5, 8] = Cell6_9;

            cells[6, 0] = Cell7_1;
            cells[6, 1] = Cell7_2;
            cells[6, 2] = Cell7_3;
            cells[7, 0] = Cell7_4;
            cells[7, 1] = Cell7_5;
            cells[7, 2] = Cell7_6;
            cells[8, 0] = Cell7_7;
            cells[8, 1] = Cell7_8;
            cells[8, 2] = Cell7_9;

            cells[6, 3] = Cell8_1;
            cells[6, 4] = Cell8_2;
            cells[6, 5] = Cell8_3;
            cells[7, 3] = Cell8_4;
            cells[7, 4] = Cell8_5;
            cells[7, 5] = Cell8_6;
            cells[8, 3] = Cell8_7;
            cells[8, 4] = Cell8_8;
            cells[8, 5] = Cell8_9;

            cells[6, 6] = Cell9_1;
            cells[6, 7] = Cell9_2;
            cells[6, 8] = Cell9_3;
            cells[7, 6] = Cell9_4;
            cells[7, 7] = Cell9_5;
            cells[7, 8] = Cell9_6;
            cells[8, 6] = Cell9_7;
            cells[8, 7] = Cell9_8;
            cells[8, 8] = Cell9_9;
        }

        
        //loads the data to the board
        public void LoadData()
        {
            for(int i=0; i<9; i++)
                for(int j=0; j<9; j++)
                {
                    //checks if the current board spot is not empty
                    if (ThePuzzleGenerator.BoardCurrent9x9[i, j] != 0)
                    {
                        //changes button text to match stored data
                        cells[i, j].Text = ThePuzzleGenerator.BoardCurrent9x9[i, j].ToString();                  
                    }
                    else
                        cells[i, j].Text = "";

                    //checks if data was orignially generated 
                    //if so disables button and greys it out  
                    if (ThePuzzleGenerator.BoardInitial9x9[i, j] != 0)
                    {
                        cells[i, j].Text = ThePuzzleGenerator.BoardInitial9x9[i, j].ToString();
                        ThePuzzleGenerator.BoardCurrent9x9[i, j] = ThePuzzleGenerator.BoardInitial9x9[i, j];
                        cells[i, j].Enabled = false;
                        cells[i, j].BackColor = Color.LightGray;
                    } 

                    //checks if the current spot matches the answer 
                    //if not places into array and counter for trancking completeness 
                    //and for help button
                    if (ThePuzzleGenerator.BoardCurrent9x9[i, j].CompareTo
                            (ThePuzzleGenerator.BoardAnswer9x9[i, j]) != 0)
                    {
                        MainGameInputClass.wrongCellsList[i, j] = cells[i, j];
                        MainGameInputClass.wrongAnswerCount++;
                    }
                }
        }
        #endregion

        #region form closing events
        private void _MainGameScreen9x9_FormClosing(object sender, FormClosingEventArgs e)
        {
            //saves the data if the board isn't complete and wipes it if it is complete
            if(MainGameInputClass.wrongAnswerCount <= 0)
            {
                ThePuzzleGenerator.BoardAnswer9x9 = null;
                ThePuzzleGenerator.BoardInitial9x9 = null;
                ThePuzzleGenerator.BoardCurrent9x9 = null;               
                FileManager.youWon = true;
                
            }
            Timer.Enabled = false;
            FileManager.SaveData();
        }

        private void MainGameScreen9x9_FormClosed(object sender, FormClosedEventArgs e)
        {
            //creates main menu form and sends it the loginparent
            //login parent is login form which is the main form
            MainMenu theMainMenu = new MainMenu(loginParent);
            theMainMenu.Show();
        }
        #endregion

        private void Timer_Tick(object sender, EventArgs e)
        {
            //uses timer tick event to update time and display it
            FileManager.currentTimeSecond++;
            if (FileManager.currentTimeSecond == 60)
            {
                FileManager.currentTimeMinute++;
                FileManager.currentTimeSecond = 0;
            }
            Time.Text = string.Format("{0:00}:{1:00}", FileManager.currentTimeMinute, FileManager.currentTimeSecond);
            this.Refresh();
        }

        private void MerceyReturn_Click(object sender, EventArgs e)
        {
            //when the mercy button is clicked return to mainmenu
            this.Close();
        }
    }
}
